package com.voxel.sense.infomsg.view.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.voxel.sense.infomsg.module.task.execution.report.details.ReportAdapter;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;


public class ReportItemRecylerItemDivider extends RecyclerView.ItemDecoration {

    private Drawable mBigDivider;
    private Drawable mDivider;

    public ReportItemRecylerItemDivider(int resId, int bigResId) {
        mDivider = Factory.app().getResources().getDrawable(resId);
        mBigDivider = Factory.app().getResources().getDrawable(bigResId);

    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        drawHorizontalLine(c, parent, state);
    }


    //画横线, 这里的parent其实是显示在屏幕显示的这部分
    public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            //获取dapter
            int childAdapterPosition = parent.getChildAdapterPosition(child);
            ReportAdapter adapter = (ReportAdapter) parent.getAdapter();

            //如果是第二级view且是该组中最后一个，divider设置比其它的item更宽些
            if (isBigDivider(adapter, childAdapterPosition)) {
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int top = child.getBottom() + params.bottomMargin;
                final int bottom = top + mBigDivider.getIntrinsicHeight();
                mBigDivider.setBounds(left, top, right, bottom);
                mBigDivider.draw(c);

            } else  {
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int top = child.getBottom() + params.bottomMargin;
                final int bottom = top + mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);

            }
        }
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//        if (parent.getChildPosition(view) != 0)
//            outRect.top = Utils.dp2px(Factory.app().getInstance(), 10);
//        if (parent.getChildPosition(view) != 0)
        final int childCount = parent.getChildCount();
//        for (int i = 0; i < childCount; i++) {
//            int childAdapterPosition = parent.getChildAdapterPosition(view);
//            if (childAdapterPosition == tipPosition && tipPosition != -1) {
//                outRect.set(0,0,0,mDrawableTip.getIntrinsicHeight());
//            }else {
//                outRect.set(0,0,0,mDivider.getIntrinsicHeight());
//            }
//
//        }
        int childAdapterPosition = parent.getChildAdapterPosition(view);
        ReportAdapter adapter = (ReportAdapter) parent.getAdapter();
        if (isBigDivider(adapter, childAdapterPosition)) {
            outRect.set(0,0,0,mBigDivider.getIntrinsicHeight());
        } else {
            outRect.set(0,0,0,mDivider.getIntrinsicHeight());
        }


    }

    private boolean isBigDivider(ReportAdapter adapter, int posisiton) {
        MultiItemEntity item = adapter.getItem(posisiton);
        int itemType = item.getItemType();

        return (itemType == Constants.NORMAL_TYPE && adapter.getItem(posisiton + 1) != null
                && adapter.getItem(posisiton + 1).getItemType() == Constants.HEAD_TYPE
        );

    }
}
